package net.gyapp.meiriyiwenpro2;

import android.app.Activity;
import android.app.Application;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.support.v7.app.AppCompatDelegate;

/**
 * Created by lbc on 2016/10/29.
 */
public class DiApplication extends Application {
    public static final String DayNightMode = "DayNightMode";
    public static final String DayNightModePreferences = "DayNightModePreferences";
    SharedPreferences preferences;
    private boolean dayNightMode = false;

    public boolean isDayNightMode() {
        return dayNightMode;
    }

    public void setDayNightMode(boolean dayNightMode) {
        this.dayNightMode = dayNightMode;
        preferences.edit().putBoolean(DayNightMode, dayNightMode).commit();
    }

    /**
     * Called when the application is starting, before any activity, service,
     * or receiver objects (excluding content providers) have been created.
     * Implementations should be as quick as possible (for example using
     * lazy initialization of state) since the time spent in this function
     * directly impacts the performance of starting the first activity,
     * service, or receiver in a process.
     * If you override this method, be sure to call super.onCreate().
     */
    @Override
    public void onCreate() {
        super.onCreate();
        preferences = getSharedPreferences(DayNightModePreferences, Activity.MODE_PRIVATE);
        dayNightMode = preferences.getBoolean(DayNightMode, false);

        AppCompatDelegate.setDefaultNightMode(dayNightMode ? AppCompatDelegate.MODE_NIGHT_YES : AppCompatDelegate.MODE_NIGHT_NO);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {

        super.onConfigurationChanged(newConfig);
    }

    static {
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    }
}
